package com.gitee.loyo.interfaces.impl

import com.gitee.loyo.KSQL
import com.gitee.loyo.Utils
import com.gitee.loyo.interfaces.ParameterizeSQL
import java.lang.StringBuilder
import kotlin.reflect.jvm.kotlinProperty

abstract class BaseModelSQL<T: Any>: ParameterizeSQL {
    protected val builder: StringBuilder = StringBuilder()
    protected val parameters: MutableList<Any?> = ArrayList()

    abstract fun buildSQL()

    override fun getSQL(): String {
        return builder.toString()
    }

    override fun getParameters(): Array<Any?> {
        return parameters.toTypedArray()
    }

    companion object {

        fun eachFields(obj: Any, fn: (String, Any?) -> Unit) {
            KSQL.reflection.getFields(obj).filter { Utils.isNotObject(it.type.kotlin) }.forEach { field ->
                fn(KSQL.nameMappingStrategy.toColumnName(field.name), KSQL.reflection.getFieldValue(field, obj))
            }
        }
    }
}